package com.dnjn.happiness.biz.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.happiness.biz.domain.TrainingRecord;
import com.dnjn.happiness.biz.domain.TrainingUserVO;
import com.dnjn.happiness.biz.mapper.TrainingRecordMapper;
import com.dnjn.happiness.biz.service.ITrainingRecordService;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.system.api.RemoteDeptService;
import com.dnjn.happiness.system.api.RemoteRoleService;
import com.dnjn.happiness.system.api.domain.SysDept;
import com.dnjn.happiness.system.api.domain.SysRole;
import com.dnjn.happiness.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 培训签到记录Service业务层处理
 *
 * @author happiness
 * @date 2023-11-04
 */
@Service
public class TrainingRecordServiceImpl extends ServiceImpl<TrainingRecordMapper, TrainingRecord> implements ITrainingRecordService {

    @Autowired
    private RemoteDeptService deptService;

//    @Autowired
//    private SysRoleMapper sysRoleMapper;

    @Autowired
    private RemoteRoleService roleService;

    @Override
    public List<SysUser> forTrainingDetailsSeeFeedback(Long trainingId) {

        return baseMapper.forTrainingDetailsSeeFeedback(trainingId);
    }

    @Override
    public List<TrainingRecord> forTrainingDetailsSeeFeedbackByTrainingIdAndLeaderId(Long trainingId, Long leaderId) {

        return baseMapper.forTrainingDetailsSeeFeedbackByTrainingIdAndLeaderId(trainingId,leaderId);
    }

    @Override
    public List<TrainingUserVO> screeningTrainingSubmit(Long trainingId,Long deptId, String nickName) {

        return baseMapper.screeningTrainingSubmit(trainingId,deptId,nickName);
    }

    @Override
    public Page<TrainingRecord> goToTheTrainingDetailsView(Page<TrainingRecord> page, String status, Long trainingId) {

        return baseMapper.goToTheTrainingDetailsView(page, status, trainingId);
    }

    @Override
    public void getInfo(List<TrainingRecord> records) {
        //部门结构
        records.stream().forEach(TrainingRecord -> {
            Long deptId = TrainingRecord.getDeptId();
            Long userId = TrainingRecord.getUserId();
            if (null != deptId) {
                R<SysDept> result = deptService.getInfoById(deptId, SecurityConstants.INNER);
                if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                    TrainingRecord.setSysDept(result.getData());
                }
            }
            // 判断角色
            if (null != userId) {
                List<SysRole> sysRoles = new ArrayList<>();
                R<List<SysRole>> result = roleService.queryRole(userId, SecurityConstants.INNER);
                if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                    sysRoles = result.getData();
                }
                //List<SysRole> sysRoles = sysRoleMapper.selectRolePermissionByUserId(userId);
                sysRoles.stream().forEach(sysRole -> {
                    if (null != sysRole.getRoleKey() && sysRole.getRoleKey().contains("common")) {
                        TrainingRecord.setRole("1");
                    }
                    if (null != sysRole.getRoleKey() && !sysRole.getRoleKey().contains("common")) {
                        TrainingRecord.setRole("2");
                    }
                });
            }
        });
    }

    @Override
    public List<TrainingRecord> getResultListByTrainingId(String trainingId) {
        return baseMapper.getResultListByTrainingId(trainingId);
    }

    @Override
    public List<TrainingRecord> selectRecordsByTrainingId(String trainingId) {
        return baseMapper.selectRecordsByTrainingId(trainingId);
    }
}
